package com.ruoyi.web.controller.datastatistics;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.datastatistics.service.HomepageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 主页相关controller层
 *
 * @author TsingYu
 * @version 2.0
 * @date 2024/6/24 15:34
 */
@RestController
@RequestMapping("/homepage")
public class HomepageController extends BaseController {
    private final HomepageService homepageService;

    public HomepageController(HomepageService homepageService) {
        this.homepageService = homepageService;
    }

    /**
     * 小表主页数据-基础
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageDataForXb")
    public AjaxResult homepageDataForXb() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getHomepageDataForXb(map));
    }

    /**
     * 小表主页数据-抄回数据
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageCopyDataForXb")
    public AjaxResult homepageCopyDataForXb(Integer days) {
        Map<String, Object> map = commonMapSetup();
        map.put("days", days);
        return success(homepageService.getHomepageCopyDataForXb(map));
    }

    /**
     * 小表主页数据-月用量数据
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageMonthUsageDataForXb")
    public AjaxResult homepageMonthUsageDataForXb(String beginDate, String endDate) {
        Map<String, Object> map = commonMapSetup();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = YearMonth.now().minusMonths(6).toString();
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = YearMonth.now().minusMonths(1).toString();
        }
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return success(homepageService.getHomepageMonthUsageDataForXb(map));
    }

    /**
     * 小表主页数据-日用量数据
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageDayUsageDataForXb")
    public AjaxResult homepageDayUsageDataForXb(String beginDate, String endDate) {
        Map<String, Object> map = commonMapSetup();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = LocalDate.now().minusDays(7).toString();
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = LocalDate.now().minusDays(1).toString();
        }
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return success(homepageService.getHomepageDayUsageDataForXb(map));
    }

    /**
     * 小表主页数据-根据小表类型统计小表数量
     *
     * @return AjaxResult
     */
    @GetMapping("/xbCountByType")
    public AjaxResult xbCountByType() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getXbCountByType(map));
    }

    /**
     * 小表主页数据-昨日日用量排名前N的设备
     *
     * @return AjaxResult
     */
    @GetMapping("/usageTop10ForXb")
    public AjaxResult usageTop10ForXb(Integer n) {
        Map<String, Object> map = commonMapSetup();
        if (n == null) {
            n = 10;
        }
        map.put("n", n);
        return success(homepageService.getYesterdayXbUsageTopN(map));
    }

    /**
     * 大表主页数据-基础
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageDataForDb")
    public AjaxResult homepageDataForDb() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getHomepageDataForDb(map));
    }


    /**
     * 大表主页数据-昨日日用量排名前N的设备
     *
     * @return AjaxResult
     */
    @GetMapping("/usageTop10ForDb")
    public AjaxResult usageTop10ForDb(Integer n) {
        Map<String, Object> map = commonMapSetup();
        if (n == null) {
            n = 10;
        }
        map.put("n", n);
        return success(homepageService.getYesterdayDbUsageTop10(map));
    }

    /**
     * 大表主页数据-昨日夜间小流排名前十的设备
     *
     * @return AjaxResult
     */
    @GetMapping("/nightMinFlowTop10ForDb")
    public AjaxResult nightMinFlowTop10ForDb() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getYesterdayDbNightMinFlowTop10(map));
    }


    /**
     * 大表主页数据-月用量数据
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageMonthUsageDataForDb")
    public AjaxResult homepageMonthUsageDataForDb(String beginDate, String endDate) {
        Map<String, Object> map = commonMapSetup();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = YearMonth.now().minusMonths(6).toString();
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = YearMonth.now().minusMonths(1).toString();
        }
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return success(homepageService.getHomepageMonthUsageDataForDb(map));
    }

    /**
     * 大表主页数据-日用量数据
     *
     * @return AjaxResult
     */
    @GetMapping("/homepageDayUsageDataForDb")
    public AjaxResult homepageDayUsageDataForDb(String beginDate, String endDate) {
        Map<String, Object> map = commonMapSetup();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = LocalDate.now().minusDays(7).toString();
        }
        if (StringUtils.isBlank(endDate)) {
            endDate = LocalDate.now().minusDays(1).toString();
        }
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return success(homepageService.getHomepageDayUsageDataForDb(map));
    }

    /**
     * 大屏设备数据
     *
     * @return AjaxResult
     */
    @GetMapping("/terminalDataForBigScreen")
    public AjaxResult terminalDataForBigScreen() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getTerminalDateGroupBySheetsId(map));
    }

    /**
     * 大屏基础数据
     *
     * @return AjaxResult
     */
    @GetMapping("/dataForBigScreen")
    public AjaxResult dataForBigScreen() {
        Map<String, Object> map = commonMapSetup();
        return success(homepageService.getDataForBigScreen(map));
    }

    /**
     * 通用的map数据装配
     *
     * @return map
     */
    private Map<String, Object> commonMapSetup() {
        Map<String, Object> map = new HashMap<>(10);
        SysUser user = getLoginUser().getUser();
        if (!Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_ADMIN)) {
            Long userId = user.getUserId();
            map.put("sysUserId", userId);
        }
        if (Objects.equals(user.getSystemFlag(), Constants.SYS_FLAG_NORMAL_USER)) {
            map.put("tenantId", user.getTenantId());
        }
        HttpServletRequest request = ServletUtils.getRequest();
        String sheetsCode = request.getParameter("sheetsCode");
        map.put("sheetsCode", sheetsCode);
        return map;
    }


    /**
     * 户表昨日区域日用水量排名前五的区域
     */
    @GetMapping("/yesterdayAreaTop5ForDosageXb")
    public AjaxResult yesterdayAreaTop5ForDosage(Integer n){
        Map<String, Object> map = commonMapSetup();
        if(n == null){
            n = 5;
        }
        map.put("n", n);
        return success(homepageService.getYesterdayAreaTop5ForDosageXb(map));
    }
}
